package net.jacqeush.mini.rpc.client.manage;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @author Jacquesh
 * @version 1.0
 * @date 2020/10/17 3:43
 */
public class FutureSync extends AbstractQueuedSynchronizer {

    private static final long serialVersionUID = -3989844522545731058L;

    private final int done = 1;

    private final int pending = 0;

    @Override
    protected boolean tryAcquire(int acquires) {
        return getState() == done;
    }

    @Override
    protected boolean tryRelease(int releases) {
        if(getState() == pending) {
            if(compareAndSetState(pending, done)) {
                return true;
            }
        }
        return false;
    }

    public boolean isDone() {
        return getState() == done;
    }
}